﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SLWebApp.ChildWindows
{
    public partial class ChildWindow_FlightsInfo : ChildWindow
    {
        #region  properties

        public FlightInfo ParentWindows
        {
            set;
            get;
        }

        public OperationType OperationType { set; get; }


        public ServiceReference_HB.WEB_VM_FLIGHTS Model { set; get; }


        #endregion

        #region cstor


        public ChildWindow_FlightsInfo()
        {
            InitializeComponent();
            progressbar.IsBusy = true;
        }

        public ChildWindow_FlightsInfo(FlightInfo parent, OperationType type= SLWebApp.OperationType.Add, ServiceReference_HB.WEB_VM_FLIGHTS parentModel = null)
            : this()
        {
            this.ParentWindows = parent;
            InitDate();

      
            progressbar.IsBusy = false;
            this.OperationType = type;
            if (type == SLWebApp.OperationType.Add)
            {
                Model = model;
            }
            else
            {
                Model = parentModel;
            }
            DataContext = Model;
           
        }

        #endregion


        #region event handler

        private void btnSave_MouseEnter(object sender, MouseEventArgs e)
        {
            progressbar.IsBusy = true;
            ServiceReference_HB.ServiceForSLClient services = new ServiceReference_HB.ServiceForSLClient();
            ServiceReference_HB.MainSeaRoutes seaRouts = cmbSL_second.SelectedItem as ServiceReference_HB.MainSeaRoutes;
            ServiceReference_HB.MainSeaRoutes terminal = cmbTerminal.SelectedItem as ServiceReference_HB.MainSeaRoutes;
            string schedule = cmbSchedule.SelectedItem as string;
            if (schedule != null)
            {
                Model.FLT_SCHDUL = schedule;
            }
            if (seaRouts != null)
                Model.FLT_SLN_ID = seaRouts.ID;
            if (terminal != null)
            {
                Model.FLT_TER_CST_ID = terminal.ID;
                //Model.FLT_SCHDUL = terminal.RouteName;
                Model.TER_NAME = terminal.RouteName;
            }

            services.IUDForFlightAsync((int)this.OperationType,  ParentWindows.UserID, Model);
            services.IUDForFlightCompleted+=(s1, e1)=>
                {

                    string result = e1.Result;
                    if (result[0] == '1')
                    {
                        if (this.OperationType == SLWebApp.OperationType.Add)
                        {
                            ParentWindows.QueryByCondition();
                        }
                        this.Close();
                       
                    }
                    else
                    {
                        MessageBox.Show(result.Substring(3));
                        progressbar.IsBusy = false;
                    }
                };

        }

        private void cmbSL_second_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference_HB.ServiceForSLClient clientServices = new ServiceReference_HB.ServiceForSLClient();
            if (e.AddedItems.Count > 0)
            {
                ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                clientServices.GetTermailAsync(item.ID);
                clientServices.GetTermailCompleted += (s1, e1) =>
                {
                    ObservableCollection<ServiceReference_HB.MainSeaRoutes> lists = e1.Result as ObservableCollection<ServiceReference_HB.MainSeaRoutes>;
                    cmbTerminal.ItemsSource = lists;
                    cmbTerminal.SelectedItem = (from reslt in lists
                                                where reslt.ID == Model.FLT_TER_CST_ID
                                                select reslt).FirstOrDefault();
                };
            }
        }


        /// <summary>
        /// 主航线更改的的时候，此航线跟着联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbmSL_first_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference_HB.ServiceForSLClient clientServices = new ServiceReference_HB.ServiceForSLClient();
            if (e.AddedItems.Count > 0)
            {
                ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                clientServices.GetChildSeaRoutes_ByLineIDAsync(ParentWindows.UserID
                    , ParentWindows.UserType, item.ID);

                clientServices.GetChildSeaRoutes_ByLineIDCompleted += (s1, e1) =>
                {
                    ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e1.Result;
                    cmbSL_second.ItemsSource = col;
                    cmbSL_second.SelectedItem = (from result in e1.Result
                                                 where result.ID == Model.FLT_SLN_ID
                                                 select  result).FirstOrDefault();

                };
            }
        }


        #endregion


        #region  private methods


        private void InitDate()
        {
            ServiceReference_HB.ServiceForSLClient clientServices = new ServiceReference_HB.ServiceForSLClient();

            ///填充主航线
            clientServices.GetMainSeaRoutes_ByUserIdAsync(ParentWindows.UserID, ParentWindows.UserType);
            clientServices.GetMainSeaRoutes_ByUserIdCompleted += (s, e) =>
            {
                ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
                cbmSL_first.ItemsSource = col;
             
             cbmSL_first.SelectedItem=   (from result in e.Result
                                          where result.ID == Model.SLN_SRT_ID
                 select result).FirstOrDefault();
            };

            //绑定班期
            clientServices.GetFltScheduleAsync();
            clientServices.GetFltScheduleCompleted+=(s, e)=>
                {
                    cmbSchedule.ItemsSource = e.Result.Distinct();

                    cmbSchedule.SelectedItem = (from item in e.Result
                                                where item == Model.FLT_SCHDUL
                                                select item).FirstOrDefault();
                };

         

        }
        #endregion


        #region fileds

      
        private ServiceReference_HB.WEB_VM_FLIGHTS model = new ServiceReference_HB.WEB_VM_FLIGHTS() { 
        
          FLT_INITDT = DateTime.Now
        };



        #endregion

       
    }
}
